package com.mxpio.erp.workshop.controller;

import com.mxpio.erp.common.workshop.entity.*;
import com.mxpio.erp.common.workshop.service.ManufactureOrderRouteService;
import com.mxpioframework.common.vo.Result;
import com.mxpioframework.jpa.policy.CrudContext;
import com.mxpioframework.jpa.policy.impl.SmartCrudPolicyAdapter;
import com.mxpioframework.jpa.query.Criteria;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.*;

import java.io.UnsupportedEncodingException;
import java.util.List;

@Tag(name = "ManufactureOrderRouteController", description = "生产订单工艺路线接口")
@RestController
@RequestMapping("/erp/workshop/moroute/")
public class ManufactureOrderRouteController {

	@Autowired
	private ManufactureOrderRouteService service;
    

    @GetMapping("list")
    @Operation(summary = "生产订单工艺路线列表", description = "获取生产订单工艺路线订单列表（无分页）", method = "GET")
    public Result<List<ManufactureOrderRoute>> list(@Parameter(description="查询参数") Criteria criteria) throws UnsupportedEncodingException {
        List<ManufactureOrderRoute> manufactureOrderItems = service.list(ManufactureOrderRoute.class, criteria);
        return Result.OK(manufactureOrderItems);
    }

    @GetMapping("page")
    @Operation(summary = "生产订单工艺路线列表", description = "获取生产订单工艺路线列表（分页）", method = "GET")
    public Result<Page<ManufactureOrderRoute>> page(@Parameter(description="查询参数") Criteria criteria, @Parameter(description="每页条数")Integer pageSize, @Parameter(description="页号")Integer pageNo) throws UnsupportedEncodingException {
        Pageable page = PageRequest.of(pageNo-1, pageSize);
        Page<ManufactureOrderRoute> manufactureOrderItems = service.listPage(ManufactureOrderRoute.class, page, criteria);
        return Result.OK(manufactureOrderItems);
    }
    @GetMapping("list/{bizNo}")
    @Operation(summary = "根据bizNo获取生产订单工艺路线", description = "根据bizNo获取生产订单工艺路线", method = "GET")
    public Result<List<ManufactureOrderRoute>> getById(@Parameter(description="业务单号") @PathVariable(name = "bizNo", required = true) String bizNo) {
        List<ManufactureOrderRoute> manufactureOrderItem = service.list(bizNo);
        return Result.OK(manufactureOrderItem);
    }



    @RequestMapping("save")
    @Operation(summary = "更新生产订单工艺路线", description = "更新生产订单工艺路线")
    public Result<List<ManufactureOrderRoute>> save(@RequestBody List<ManufactureOrderRoute> manufactureOrderRouteList) {
        //service.save(manufactureOrderRouteList);
        service.save(manufactureOrderRouteList,new SmartCrudPolicyAdapter(){
            @Override
            public boolean beforeInsert(CrudContext context) {
                Object o = context.getEntity();
                if(o instanceof ManufactureOrderRouteFixture){
                    ManufactureOrderRoute route = context.getParent();
                    ((ManufactureOrderRouteFixture) o).setOrderRoutProcId(route.getId());
                }
                return true;
            }
        });
        return Result.OK(manufactureOrderRouteList);
    }

}
